package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type BadreportController struct {
	BaseController
}

func (c *BadreportController) Prepare() {
	//先执行
	c.BaseController.Prepare()
}

//商品报损
func (c *BadreportController) Badreport() {
	c.checkAuthor("badreport/badreport")

	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.ErpBadreporteQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.DepartmentId = c.curUser.DepartmentId
		data, total := models.ErpBadreportPageList(&params)
		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/badreport/badreport"
	c.display("", "badreport/badreport_footerjs.html")
}

//报损单基本信息
func (c *BadreportController) ChooseTrader() {
	c.checkAuthor("badreport/badreport")
	if c.Ctx.Input.IsPost() {
		remarks := c.GetString("Remarks")
		badreport := new(models.ErpBadreport)
		badreport.DepartmentId = c.curUser.DepartmentId
		badreport.Remarks = remarks
		badreport.BadreportUser = c.curUser.RealName
		badreport.BadreportTime = time.Now().Unix()
		badreport.Auditing = 0
		badreport.IsTemp = 1 //临时数据
		ser := services.BadreportService{}
		if id, err := ser.CreateBadreport(badreport); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", id)
		}
	}
	c.display("", "badreport/choosetrader_footerjs.html", "shared/layout_dialog.html")
}

//报损单删除
func (c *BadreportController) BadreportDelete() {
	id, _ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	ser := services.BadreportService{}
	if err := ser.DeleteBadreport(c.curUser.DepartmentId, id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//报损单商品添加
func (c *BadreportController) BadreportAdd() {
	c.checkAuthor("badreport/badreport")
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误1")
	}
	if c.Ctx.Input.IsPost() {
		ser := services.BadreportService{}
		if err := ser.SubmitBadreport(c.curUser.DepartmentId, id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
	badreport, err := models.GetErpBadreportById(id)
	if err != nil {
		c.pageError("参数错误2")
	}
	if badreport.DepartmentId != c.curUser.DepartmentId {
		c.pageError("找不到单据信息")
	}
	c.Data["m"] = badreport;
	c.Data["activeSidebarUrl"] = "/badreport/badreport"
	c.showErpTypeid()
	c.display("", "badreport/badreportadd_footerjs.html")
}

//添加报损原因
func (c *BadreportController) BadreportAddToGoods() {

	entry_id, _ := c.GetInt64("entry_id")
	inv_id, _ := c.GetInt64("inv_id")

	if c.Ctx.Input.IsPost() {
		badreport_num, _ := c.GetInt64("badreport_num")
		badreport_reson := c.GetString("badreport_reson")
		ser := services.BadreportService{}
		if err := ser.AddBadreportData(entry_id, inv_id, badreport_num, badreport_reson); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", "")
		}
	}

	if entry_id == 0 || inv_id == 0 {
		c.pageError("参数错误")
	}
	c.display("", "badreport/badreportaddtogoods_footerjs.html", "shared/layout_dialog.html")
}

//获取已添加原因&删除原因
func (c *BadreportController) BadreportGetToGoods() {
	badreport_id, _ := c.GetInt64("badreport_id")
	if badreport_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	if c.Ctx.Input.IsPost() { //删除
		badreport_data_id, _ := c.GetInt64("badreport_data_id")
		if badreport_data_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		}
		ser := services.BadreportService{}
		if err := ser.DeleteBadreportData(badreport_id, badreport_data_id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	} else { //获取明细
		params := models.ErpBadreportDataQueryParam{}
		params.Limit = 1000
		params.Badreport = badreport_id
		params.RelateSel = []interface{}{"Inventory", "Inventory__Products", "Inventory__Acceptance","Inventory__Snd"}
		data, _ := models.ErpBadreportDataPageList(&params)
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}
}

//查看报损单据
func (c *BadreportController) BadreportView() {
	c.checkAuthor("badreport/badreport")

	if c.Ctx.Input.IsPost() {
		params := models.ErpBadreportDataQueryParam{}
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Limit = -1
		params.RelateSel = []interface{}{"Inventory", "Inventory__Products", "Inventory__Acceptance","Inventory__Snd"}
		data, _ := models.ErpBadreportDataPageList(&params)
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误")
	}
	badreport, err := models.GetErpBadreportById(id)
	if err != nil {
		c.pageError("参数错误")
	}
	if badreport.DepartmentId != c.curUser.DepartmentId {
		c.pageError("找不到单据信息")
	}
	c.Data["m"] = badreport;
	c.Data["activeSidebarUrl"] = "/badreport/badreport"

	c.display("", "badreport/badreportview_footerjs.html")
}

//查看报损单据审核
func (c *BadreportController) BadreportAuditing() {
	badreport_id, _ := c.GetInt64("badreport_id")
	status := c.GetString("status")
	ser := services.BadreportService{}
	if err := ser.BadreportAuditing(&c.curUser, badreport_id, status); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//查看所有报损商品
func (c *BadreportController) ViewGoodsAll() {
	c.checkAuthor("badreport/badreport")

	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.ErpBadreportDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.DepartmentId = c.curUser.DepartmentId
		params.Auditing = 3
		params.RelateSel = []interface{}{"Badreport", "Inventory", "Inventory__Products", "Inventory__Acceptance","Inventory__Snd"}
		data, total := models.ErpBadreportDataPageList(&params)
		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/badreport/badreport"
	c.display("", "badreport/viewgoodsall_footerjs.html")
}

//报损单执行
func (c *BadreportController) BadreportExec() {
	c.checkAuthor("badreport/badreport_exec")

	if c.Ctx.Input.IsPost() {
		var params models.ErpBadreporteQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.DepartmentId = c.curUser.DepartmentId
		params.Auditing = 1
		data, total := models.ErpBadreportPageList(&params)
		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/badreport/badreport_exec"
	c.display("", "badreport/badreportexec_footerjs.html")
}

//报损单执行
func (c *BadreportController) BadreportExecView() {
	c.checkAuthor("badreport/badreport_exec")

	if c.Ctx.Input.IsPost() {
		params := models.ErpBadreportDataQueryParam{}
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Limit = -1
		params.RelateSel = []interface{}{"Inventory", "Inventory__Products", "Inventory__Acceptance","Inventory__Snd"}
		data, _ := models.ErpBadreportDataPageList(&params)
		c.jsonResult(enums.JRCodeSucc, "ok", data)
	}
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误")
	}
	badreport, err := models.GetErpBadreportById(id)
	if err != nil {
		c.pageError("参数错误")
	}
	if badreport.DepartmentId != c.curUser.DepartmentId {
		c.pageError("找不到单据信息")
	}
	if badreport.Auditing != 1 {
		c.pageError("单据不允许操作")
	}

	c.Data["m"] = badreport;

	c.Data["activeSidebarUrl"] = "/badreport/badreport_exec"
	c.display("", "badreport/badreportexecview_footerjs.html")
}

//执行&驳回单据
func (c *BadreportController) BadreportExecExactly() {
	badreport_id, _ := c.GetInt64("badreport_id")
	status := c.GetString("status")
	ser := services.BadreportService{}
	if err := ser.BadreportExecExactly(&c.curUser, badreport_id, status); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}